package ua.kpi_java_training5.alekseenko.src.module1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

/**
 *  The class finds palindromes from string
 *
 *  @autor Aleksey Alekssenko
 *  @version 1.10
 *  @since 2013-05-14
 *
 */
public class FindPalindrome {

    /**
     *
     */
    public static void main(String[] args) throws IOException {
        String s = input();
        output(findAllPalindromes(s));
    }

    /**
     * Inputs string for finding
     *
     * @return
     * @throws IOException
     */
    public static String input() throws IOException{
        BufferedReader b = new BufferedReader( new InputStreamReader(System.in));
        String string = b.readLine();
        return string;
    }

    /**
     * Finds possible palindromes around chars of string
     *
     * @param string
     * @return all possible palindromes from string in list
     */
    public static ArrayList<String> findAllPalindromes(String string){
        char [] stringByChar = string.toCharArray();
        ArrayList <String> allPalindromes = new ArrayList <String>();
        ArrayList <String> palindromesForEachI = new ArrayList <String>();
        for (int i = 1; i < stringByChar.length-2; i++){

            palindromesForEachI.addAll(foundPalindromes(stringByChar, i, i + 1));
            palindromesForEachI.addAll(foundPalindromes(stringByChar, i, i + 2));
            allPalindromes = doesntMeetPalindromeLength(palindromesForEachI);
        }
        return allPalindromes;
    }

    public static ArrayList<String> doesntMeetPalindromeLength(ArrayList<String> palindromesForEachI) {
        ArrayList<String> allPalindromes = new ArrayList<String>();
        for (int i = 0; i < palindromesForEachI.size(); i++) {
            if (palindromesForEachI.get(i).length() > 2)    {
                allPalindromes.add(palindromesForEachI.get(i));
            }
        }
        return  allPalindromes;
    }

    /**
     * Finds possible palindromes in set char of string
     *
     * @param stringByChar
     * @param currentlyChar set char
     * @param mirrorChar char mirror to currentlyChar
     * @return found palindromes
     */
    public static  ArrayList<String> foundPalindromes(char[] stringByChar, int currentlyChar, int mirrorChar){
        ArrayList <String> palindromesForEachI = new ArrayList <String>();
        String palindrome = "";
        if ((mirrorChar - currentlyChar) == 2){
            palindrome += stringByChar[mirrorChar-1];
        }
        while (stringByChar[currentlyChar] == stringByChar[mirrorChar]){
            palindrome = stringByChar[currentlyChar] + palindrome + stringByChar[mirrorChar];
            palindromesForEachI.add(palindrome);
            currentlyChar--;
            mirrorChar++;
            if(currentlyChar < 0 || mirrorChar >= stringByChar.length){
                break;
            }
        }
        return palindromesForEachI;
    }

    /**
     * Outputs all found palindromes
     *
     * @param allPalindromes
     */
    public static void output(ArrayList<String> allPalindromes){
        for (String e : allPalindromes){
            System.out.println(e);
        }
    }
}
